package org.baicaixiaozhan.learn.gson;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.baicaixiaozhan.learn.guava.domain.Person;

import java.util.HashMap;
import java.util.Map;

/**
 * DESC: {@link GsonBuilder} 序列化 null 示例
 *
 * @author baicaixiaozhan
 * @since 2020/11/14
 */
public class GsonDemo {

    public static void main(String[] args) {
        Person person = new Person(null, null, "深圳");

        Gson gson = new Gson();
        System.out.println(gson.toJson(person));

        Map<String, String> hashMap = Maps.newHashMap();
        hashMap.put("firstName", null);
        hashMap.put("lastName", null);
        hashMap.put("address", "深圳");
        System.out.println(gson.toJson(hashMap));


        Gson gson1 = new GsonBuilder().serializeNulls().create();
        System.out.println(gson1.toJson(person));
        System.out.println(gson1.toJson(hashMap));
    }
}
